import pygame
import sys
import random

black_color = 0, 0, 0
red_color = 255, 0, 0
blue_color = 0, 0, 255
yellow_color = 255, 255, 0
size = width, height = 640, 480


def game_over():
    """ 游戏结束 """
    print('游戏结束！')
    print('-' * 30)
    pygame.quit()
    sys.exit()


def main():
    """ 程序主函数 """
    pygame.init()
    screen = pygame.display.set_mode(size)
    pygame.display.set_caption('Python简易贪吃蛇')
    fps_clock = pygame.time.Clock()

    snake_position = [100, 100]  # 蛇的位置
    snake_body = [[100, 100], [80, 100], [60, 100]]
    target_position = [300, 300]  # 方块位置
    speed = 10
    target_flag = 1  # 方块是否被吃标识
    direction = 'right'  # 运动方向 初始向右
    fen = 0

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                print('-' * 30)
                print('点击关闭按钮，', end='')
                game_over()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    if direction != 'right':
                        direction = 'left'
                elif event.key == pygame.K_RIGHT:
                    if direction != 'left':
                        direction = 'right'
                elif event.key == pygame.K_UP:
                    if direction != 'down':
                        direction = 'up'
                elif event.key == pygame.K_DOWN:
                    if direction != 'up':
                        direction = 'down'
                elif event.key == pygame.K_ESCAPE:
                    print('-' * 30)
                    print('按下键盘Esc，', end='')
                    game_over()

        if direction == 'left':
            snake_position[0] -= 20
        elif direction == 'right':
            snake_position[0] += 20
        elif direction == 'up':
            snake_position[1] -= 20
        elif direction == 'down':
            snake_position[1] += 20

        snake_body.insert(0, list(snake_position))  # 增加新的蛇头位置

        if snake_position[0] == target_position[0] and snake_position[1] == target_position[1]:
            fen += 1
            pygame.display.set_caption('Python简易贪吃蛇 --- 游戏得分：' + str(fen))
            print('-' * 30)
            print('成功吃到一个方块，当前得分为 %d' % fen)
            target_flag = 0  # 如果吃到方块 标识改变
        else:
            snake_body.pop()  # 如果没吃到方块 删除蛇尾

        # 如果方块被吃 方块标识等于0
        if target_flag == 0:
            # 窗口内随机位置生成方块
            x = random.randrange(1, 32)
            y = random.randrange(1, 24)

            while [x * 20, y * 20] in snake_body:
                print('-' * 30)
                print('方块和蛇身重合，重新生成方块坐标！')
                x = random.randrange(1, 32)
                y = random.randrange(1, 24)

            target_position = [x * 20, y * 20]
            # 方块标识重新设定为 1
            target_flag = 1

        screen.fill(black_color)  # 填充背景
        for position in snake_body:
            if position == snake_body[0]:
                pygame.draw.rect(screen, red_color, (position[0], position[1], 20, 20), 2)
            else:
                pygame.draw.rect(screen, yellow_color, (position[0], position[1], 20, 20), 2)
            pygame.draw.rect(screen, blue_color, (target_position[0], target_position[1], 20, 20), 2)

        pygame.display.update()  # 更新屏幕

        if snake_position[0] < 0 or snake_position[0] == width:  # 如果蛇头接触左右墙壁 游戏结束
            print('-' * 30)
            print('蛇头接触左右边缘，', end='')
            game_over()
        if snake_position[1] < 0 or snake_position[1] == height:  # 如果蛇头接触上下墙壁 游戏结束
            print('-' * 30)
            print('蛇头接触上下边缘，', end='')
            game_over()
        if snake_position in snake_body[1:]:  # 如果蛇头接触蛇身 游戏结束
            print('-' * 30)
            print('蛇头接触蛇身，', end='')
            game_over()

        fps_clock.tick(speed)


if __name__ == '__main__':
    main()
